package io.renren.modules.medical.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.MedicalException;
import io.renren.common.result.ResultCodeEnum;
import io.renren.common.utils.Constant;
import io.renren.modules.medical.dao.VisitRecordStepDao;
import io.renren.modules.medical.entity.VisitRecordEntity;
import io.renren.modules.medical.entity.VisitRecordStepEntity;
import io.renren.modules.medical.service.VisitRecordStepService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service("visitRecordStepService")
public class VisitRecordStepServiceImpl extends ServiceImpl<VisitRecordStepDao, VisitRecordStepEntity> implements VisitRecordStepService {

    /**
     * 更新步骤表
     */
    @Transactional
    @Override
    public void updateStep(VisitRecordStepEntity visitRecordStepEntity) {
        // 先查看传来的数据的上一阶段状态是否为1或2
        Long id = visitRecordStepEntity.getId();
        VisitRecordStepEntity recordStepEntity = this.getById(id);
        if (recordStepEntity.getVisitStep() == 1){
            this.updateById(visitRecordStepEntity);
            return;
        }
        // 获取到上一级
        LambdaQueryWrapper<VisitRecordStepEntity> visitRecordStepWrapper = new LambdaQueryWrapper<>();
        visitRecordStepWrapper.eq(VisitRecordStepEntity::getVisitStep, recordStepEntity.getVisitStep() - 1);
        visitRecordStepWrapper.eq(VisitRecordStepEntity::getVisitRecordId, recordStepEntity.getVisitRecordId());
        VisitRecordStepEntity lastOne = this.getOne(visitRecordStepWrapper);
        if (lastOne == null){
            throw new MedicalException(ResultCodeEnum.NOT_FIND_ERROR.getCode(), ResultCodeEnum.NOT_FIND_ERROR.getMessage());
        }
        if (lastOne.getStatus() == 0){
            throw new MedicalException(ResultCodeEnum.UPDATE_RECORD_ERROR.getCode(), "请先完成上阶段内容");
        }
        // 保证了每个阶段不存在先完成后面阶段的问题
        this.updateById(visitRecordStepEntity);
    }

    @Override
    public List<VisitRecordStepEntity> getStepList(Long id) {
        LambdaQueryWrapper<VisitRecordStepEntity> visitRecordStepWrapper = new LambdaQueryWrapper<>();
        visitRecordStepWrapper.eq(VisitRecordStepEntity::getVisitRecordId, id);
        return this.list(visitRecordStepWrapper);
    }

    /**
     * 删除阶段表信息
     */
    @Transactional
    @Override
    public void removeStep(Long stepId) {
        // 先查看该信息是不是被回复过的
        VisitRecordStepEntity stepEntity = this.getById(stepId);
        if (stepEntity == null){
            throw new MedicalException(ResultCodeEnum.NOT_FIND_ERROR.getCode(), "要删除的信息找不到");
        }
        if (stepEntity.getStatus() == Constant.VisitStepStatus.FINISHED.getValue()){
            throw new MedicalException(ResultCodeEnum.DELETED_STEP_ERROR.getCode(), "该阶段数据已完成，无法删除");
        }
        // 重新排序阶段表顺序
        this.removeById(stepId);
        List<VisitRecordStepEntity> stepList = this.getStepList(stepEntity.getVisitRecordId());
        for (int i = 0; i < stepList.size(); i++){
            // 更新阶段数据
            stepList.get(i).setVisitStep(i + 1);
            this.updateById(stepList.get(i));
        }

    }


}